package com.android.webserver.db;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * @author Baptiste GOURDIN
 *
 */
public class WebApplication
{

	private String						name			= null;
	private String						path			= null;
	private String						provider	= null;
	private String						password	= null;
	private boolean						enabled		= true;
	
	private boolean						httponly	= true;
	private boolean						serverfilter	= true;
	private boolean						csp	= true;
	private boolean						scsp	= true;
	private boolean						csrf	= true;
	private boolean						originheader	= true;
	private boolean						xframe	= true;
	private boolean						ssl	= true;
	private boolean						hsts	= true;
	private boolean						securecookie	= true;
	private long									id = -1;
	
//	protected static String[]	select		= { "ROWID as _id", "name", "path",
//			"provider", "password"					};

	protected static String[]	select		= { "ROWID as _id", "name", "path",
		"provider", "password", "enabled", "httponly", "serverfilter", "csp", 
		"scsp", "csrf", "originheader", "xframe", "ssl", "hsts", "securecookie" };

	
	// XXX Allowed Referers and Origin and Host
	// XXX Entry point pages

	public WebApplication(String name, String path, String provider)
	{
		this.name = name;
		this.path = path;
		this.provider = provider;
	}

	public WebApplication(Cursor c)
	{
		id = c.getLong(0);
		name = c.getString(1);
		path = c.getString(2);
		provider = c.getString(3);
		password = c.getString(4);
		enabled = c.getString(5).equals("1")?true:false;
	 httponly = c.getString(6).equals("1")?true:false;
		serverfilter = c.getString(7).equals("1")?true:false;
		csp = c.getString(8).equals("1")?true:false;
		scsp = c.getString(9).equals("1")?true:false;
		csrf = c.getString(10).equals("1")?true:false;
		originheader = c.getString(11).equals("1")?true:false;
		xframe = c.getString(12).equals("1")?true:false;
		ssl = c.getString(13).equals("1")?true:false;
		hsts = c.getString(14).equals("1")?true:false;
		securecookie = c.getString(15).equals("1")?true:false;
	}

	public ContentValues getContentValues()
	{
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("provider", provider);
		values.put("path", path);
		values.put("password", (password != null) ? password : "");
		values.put("enabled", (enabled) ? 1 : 0);

		/* Security config parameters */
		values.put("httponly", (httponly) ? 1 : 0);
		values.put("serverfilter", (serverfilter) ? 1 : 0);
		
		values.put("csp", (csp) ? 1 : 0);
		values.put("scsp", (scsp) ? 1 : 0);

		values.put("csrf", (csrf) ? 1 : 0);
		values.put("originheader", (originheader) ? 1 : 0);

		values.put("xframe", (xframe) ? 1 : 0);
		values.put("ssl", (ssl) ? 1 : 0);

		values.put("hsts", (hsts) ? 1 : 0);
		values.put("securecookie", (securecookie) ? 1 : 0);

		return values;
	}

	@Override
	public String toString()
	{
		return ("WebApplication: " + name + " path(" + path + ") provider ("
				+ provider + ")");
	}

	public long getId()
	{
		return id;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getName()
	{
		return name;
	}

	public String getPath()
	{
		return path;
	}

	public String getProvider()
	{
		return provider;
	}
	
	public boolean getHttpOnlyStatus() { return httponly; }
	public boolean getServerFilterStatus() { return serverfilter; }
	public boolean getCspStatus() { return csp; }
	public boolean getScspStatus() { return scsp; }
	public boolean getCsrfStatus() { return csrf; }
	public boolean getOriginHeaderStatus() { return originheader; }
	public boolean getXframeStatus() { return xframe; }
	public boolean getSslStatus() { return ssl; }
	public boolean getHstsStatus() { return hsts; }
	public boolean getSecureCookieStatus() { return securecookie; }
	public boolean getEnabled() { return enabled; }
	
	public void setHttpOnlyStatus(boolean stat) { httponly = stat; }
	public void setServerFilterStatus(boolean stat) { serverfilter = stat; }
	public void setCspStatus(boolean stat) { csp = stat; }
	public void setScspStatus(boolean stat) { scsp = stat; }
	public void setCsrfStatus(boolean stat) { csrf = stat; }
	public void setOriginHeaderStatus(boolean stat) { originheader = stat; }
	public void setXframeStatus(boolean stat) { xframe = stat; }
	public void setSslStatus(boolean stat) { ssl = stat; }
	public void setHstsStatus(boolean stat) { hsts = stat; }
	public void setSecureCookieStatus(boolean stat) { securecookie = stat; }
	public void setEnabled(boolean stat) { enabled = stat; }
	
}
